package com.kehutong.wxapi.service.impl;

import com.kehutong.wxapi.request.GetLoginInfoRequest;
import com.kehutong.wxapi.request.GetUserDetail3rdRequest;
import com.kehutong.wxapi.request.GetUserInfo3rdRequest;
import com.kehutong.wxapi.request.GetUserInfoRequest;
import com.kehutong.wxapi.response.GetLoginInfoResponse;
import com.kehutong.wxapi.response.GetUserDetail3rdResponse;
import com.kehutong.wxapi.response.GetUserInfo3rdResponse;
import com.kehutong.wxapi.response.GetUserInfoResponse;
import com.kehutong.wxapi.service.OauthService;
import com.kehutong.wxapi.util.KhtClient;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSON;
import org.coraframework.json.JSONObject;

/**
 * @author: liukunlong
 * @date: 2021-03-30 20:15
 */
public class OauthServiceImpl implements OauthService {
    @Inject
    private KhtClient khtClient;
    /**
     * 获取访问用户身份
     * 该接口用于根据code获取成员信息
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
     * @param getUserInfoRequest 访问用户身份请求对象
     * @return 访问用户身份响应对象
     * @throws Exception 异常
     */
    public GetUserInfoResponse getUserInfo(GetUserInfoRequest getUserInfoRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/user/getuserinfo", http -> {
            http.addParam("access_token", getUserInfoRequest.getAccessToken());
            http.addParam("code",getUserInfoRequest.getCode());
        });
        return JSON.parseObject(jsonObject.toJSONString(), GetUserInfoResponse.class);
    }


    /**
     * 获取第三方访问用户身份
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/service/getuserinfo3rd?suite_access_token=SUITE_ACCESS_TOKEN&code=CODE
     * @param getUserInfo3rdRequest 第三方访问用户身份请求对象
     * @return 第三方访问用户身份响应对象
     * @throws Exception 异常
     */
    public GetUserInfo3rdResponse getUserInfo3rd(GetUserInfo3rdRequest getUserInfo3rdRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/service/getuserinfo3rd", http -> {
            http.addParam("suite_access_token", getUserInfo3rdRequest.getSuiteAccessToken());
            http.addParam("code",getUserInfo3rdRequest.getCode());
        });
        return JSON.parseObject(jsonObject.toJSONString(), GetUserInfo3rdResponse.class);
    }

    /**
     * 获取第三方访问用户敏感信息
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/service/getuserdetail3rd?suite_access_token=SUITE_ACCESS_TOKEN
     * @param getUserDetail3rdRequest 第三方访问用户敏感信息请求对象
     * @return 第三方访问用户敏感信息响应
     * @throws Exception 异常
     */
    public GetUserDetail3rdResponse getUserDetail3rd(GetUserDetail3rdRequest getUserDetail3rdRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/service/getuserdetail3rd", http -> {
            http.addParam("suite_access_token", getUserDetail3rdRequest.getSuiteAccessToken());
            JSONObject body=new JSONObject();
            body.put("user_ticket",getUserDetail3rdRequest.getUserTicket());
            http.setBody(body.toJSONString());
        });
        return JSON.parseObject(jsonObject.toJSONString(), GetUserDetail3rdResponse.class);
    }

    /**
     * 获取第三方扫码登录用户信息
     * 请求地址： https://qyapi.weixin.qq.com/cgi-bin/service/get_login_info?access_token=PROVIDER_ACCESS_TOKEN
     * @param getLoginInfoRequest 获取第三方扫码登录用户信息请求对象
     * @return 获取第三方扫码登录用户信息响应对象
     * @throws Exception 异常
     */
    public GetLoginInfoResponse getLoginInfo(GetLoginInfoRequest getLoginInfoRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/service/get_login_info", http -> {
            http.addParam("access_token", getLoginInfoRequest.getAccessToken());
            JSONObject body=new JSONObject();
            body.put("auth_code",getLoginInfoRequest.getAuthCode());
            http.setBody(body.toJSONString());
        });
        return JSON.parseObject(jsonObject.toJSONString(), GetLoginInfoResponse.class);
    }
}
